package com.flydog.taskManager.controller;

import com.flydog.taskManager.common.api.ApiException;
import com.flydog.taskManager.common.api.CommonResult;
import com.flydog.taskManager.common.api.PageResponse;
import com.flydog.taskManager.common.util.DateUtil;
import com.flydog.taskManager.common.vaildator.FlagValidator;
import com.flydog.taskManager.dto.PhaseTaskSearchParam;
import com.flydog.taskManager.dto.PhasedTaskParam;
import com.flydog.taskManager.dto.PhasedTaskTreeStructure;
import com.flydog.taskManager.dto.PhasedTaskWithChildren;
import com.flydog.taskManager.entity.PhasedTask;
import com.flydog.taskManager.service.PhasedTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.*;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

/**
 * @author Cao
 * @description 阶段性工作控制类
 * @create 2021-05-10
 */

@Slf4j
@RestController
@RequestMapping("/phased")
@Validated
public class PhasedTaskController {
    @Autowired
    private PhasedTaskService phasedTaskService;

    @PostMapping(value = "/create")
    CommonResult<PhasedTask> create(@RequestBody @Validated PhasedTaskParam param) {
        return CommonResult.success(phasedTaskService.createTask(param));
    }

    @PostMapping(value = "/update/{id}")
    CommonResult<PhasedTask> update(@RequestBody @Validated PhasedTaskParam param, @PathVariable("id") Long id) {
        return CommonResult.success(phasedTaskService.update(id, param));
    }

    @GetMapping(value = "/{id}")
    CommonResult<PhasedTask> getById(@PathVariable("id") Long id) {
        return phasedTaskService.getTask(id).map(CommonResult::success).orElseGet(CommonResult::failed);
    }

    @PostMapping(value = "/delete")
    CommonResult<Set<Long>> delete(@RequestParam("ids") @NotEmpty(message = "必需包含一个或多个id") Long[] ids) {
        return CommonResult.success(phasedTaskService.deleteTask(ids));
    }

    @GetMapping(value = "/list/endIn")
    CommonResult<PageResponse<PhasedTask>>
    listByEndDate(@RequestParam(value = "begin") String begin,
                  @RequestParam(value = "end") String end,
                  @RequestParam(value = "pageNum") @Min(value = 1, message = "分页页码最小值为 1") Integer pageNum,
                  @RequestParam(value = "pageSize") @Max(value = 50, message = "分页页码最大值为 50 ") Integer pageSize) throws ApiException {
        Calendar beginDate = DateUtil.initDateToStartTimeInDay(begin);
        Calendar endDate = DateUtil.initDateToStartTimeInDay(end);
        return CommonResult.success(phasedTaskService.listByEndDate(beginDate, endDate, pageNum, pageSize));
    }

    @GetMapping(value = "/listAll/endIn")
    CommonResult<List<PhasedTask>> listByEndDate(@RequestParam(value = "begin") String begin,
                                                 @RequestParam(value = "end") String end) throws ApiException {
        Calendar beginDate = DateUtil.initDateToStartTimeInDay(begin);
        Calendar endDate = DateUtil.initDateToEndTimeInDay(end);
        return CommonResult.success(phasedTaskService.listByEndDate(beginDate, endDate));
    }

    @GetMapping(value = "/list/beginIn")
    CommonResult<PageResponse<PhasedTask>>
    listByBeginDate(@RequestParam(value = "begin") String begin,
                    @RequestParam(value = "end") String end,
                    @RequestParam(value = "pageNum") @DecimalMin(value = "1", message = "分页页码最小值为 1 ") Integer pageNum,
                    @RequestParam(value = "pageSize") @DecimalMax(value = "50", message = "分页页码最大值为 50 ") Integer pageSize) throws ApiException {
        Calendar beginDate = DateUtil.initDateToStartTimeInDay(begin);
        Calendar endDate = DateUtil.initDateToEndTimeInDay(end);
        return CommonResult.success(phasedTaskService.listByBeginDate(beginDate, endDate, pageNum, pageSize));
    }

    @GetMapping(value = "/listAll/beginIn")
    CommonResult<List<PhasedTask>> listByBeginDate(@RequestParam(value = "begin") String begin,
                                                   @RequestParam(value = "end") String end) throws ApiException {

        Calendar beginDate = DateUtil.initDateToStartTimeInDay(begin);
        Calendar endDate = DateUtil.initDateToEndTimeInDay(end);
        return CommonResult.success(phasedTaskService.listByBeginDate(beginDate, endDate));
    }

    @GetMapping(value = "/listWithChildren")
    CommonResult<List<PhasedTaskWithChildren>> listWithChildren(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "target", required = false) String target,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "processStatus", required = false) Integer processStatus,
            @RequestParam(value = "importance", required = false) Integer importance,
            @RequestParam(value = "beginStart", required = false) String beginStart,
            @RequestParam(value = "beginFinish", required = false) String beginFinish,
            @RequestParam(value = "endStart", required = false) String endStart,
            @RequestParam(value = "endFinish", required = false) String endFinish
    ) throws ApiException {
        PhaseTaskSearchParam phaseTaskSearchParam = new PhaseTaskSearchParam();
        phaseTaskSearchParam.setName(name);
        phaseTaskSearchParam.setTags(tags);
        phaseTaskSearchParam.setTarget(target);
        phaseTaskSearchParam.setImportance(importance);
        phaseTaskSearchParam.setProcessStatus(processStatus);
        if (beginStart != null) {
            phaseTaskSearchParam.setBeginStart(DateUtil.initDateToStartTimeInDay(beginStart).getTime());
        }
        if (beginFinish != null) {
            phaseTaskSearchParam.setBeginFinish(DateUtil.initDateToEndTimeInDay(beginFinish).getTime());
        }
        if (endStart != null) {
            phaseTaskSearchParam.setEndStart(DateUtil.initDateToStartTimeInDay(endStart).getTime());
        }
        if (endFinish != null) {
            phaseTaskSearchParam.setEndFinish(DateUtil.initDateToEndTimeInDay(endFinish).getTime());
        }
        return CommonResult.success(phasedTaskService.listTaskWithChildren(phaseTaskSearchParam));
    }

    @PostMapping(value = "/updateProcessStatus")
    CommonResult<PhasedTask>
    updateProcessStatus(
            @RequestParam Long id,
            @RequestParam @FlagValidator(value = {0, 1, 2}, message = "工作状态格式错误只能为0, 1, 2") int status) {
        return CommonResult.success(phasedTaskService.updateProcessStatus(id, status));
    }

    @GetMapping(value = "/treeStructure/{id}")
    CommonResult<PhasedTaskTreeStructure> getTree(@PathVariable("id") Long id) {
        return CommonResult.success(phasedTaskService.getTree(id));
    }


}

